<template>
  <el-dialog
    :model-value="dialogVisible"
    title="添加用户"
    width="40%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="70px" :rules="rules">
      <el-form-item label="用户id" prop="userName">
        <el-input v-model="form.userName" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
<<<<<<< HEAD
import { defineEmits, ref, defineProps, watch } from 'vue'
import { addusertoclass } from '@/api/captain'
import { ElMessage } from 'element-plus'

//  接收传过来的原信息
const props = defineProps({
  cid: String
})

const cid = ref(props.cid)
watch(
  () => props.cid,
  () => {
    cid.value = props.cid
  }
)

=======
import { defineEmits, ref } from 'vue'
import { addusertoclass } from '@/api/captain'
import { ElMessage } from 'element-plus'
>>>>>>> 7d520118954f103433f5074ad63a16f5b63acf51
const emits = defineEmits(['update:modelValue', 'inituserlist'])
const formRef = ref(null)
const form = ref({
  username: '',
  role: 1,
<<<<<<< HEAD
  status: 1,
  id: 0
=======
  status: 1
>>>>>>> 7d520118954f103433f5074ad63a16f5b63acf51
})
const handleClose = async () => {
  emits('update:modelValue', false)
}

const handleConfirm = () => {
  console.log(form.value)
  formRef.value.validate(async (valid) => {
    if (valid) {
      ElMessage({
        message: '用户添加成功',
        type: 'success'
      })
      const joindata = ref({
<<<<<<< HEAD
        cid: cid.value,
        uid: form.value.userName
      })
      joindata.value.cid = parseInt(joindata.value.cid)
      joindata.value.uid = parseInt(joindata.value.uid)
=======
        cid: form.value.id,
        uid: form.value.userName
      })
>>>>>>> 7d520118954f103433f5074ad63a16f5b63acf51
      await addusertoclass(joindata.value)
      emits('inituserlist')
    } else {
      ElMessage({
        message: '用户添加失败',
        type: 'warning'
      })
      return false
    }
  })

  handleClose()
}
const rules = ref({
  username: [
    { required: true, message: '请输入有效用户名', trigger: 'blur' },
    { min: 3, max: 15, message: 'Length should be 3 to 5', trigger: 'blur' }
  ],
  password: [
    {
      required: true,
      message: '请输入有效密码',
      trigger: 'change'
    }
  ]
})
</script>

<style lang="scss" scoped></style>
